package pikes.samples.countries.ui.peak;

import java.util.Map;

import org.springframework.web.servlet.support.RequestContext;

import pikes.ecma.IAssignmentExpression;
import pikes.ecma.VariableStatement;
import pikes.peak.RequestIndependentProperty;
import pikes.peak.RequestProperty;

public class DynamicVariableStatement implements RequestProperty<VariableStatement> {

	private CharSequence identifier = null;

	private RequestProperty<? extends IAssignmentExpression> expression = null;

	public DynamicVariableStatement(CharSequence identifier) {
		this.identifier = identifier;
	}

	public DynamicVariableStatement(CharSequence identifier, IAssignmentExpression expression) {
		this(identifier, new RequestIndependentProperty<IAssignmentExpression>(expression));
	}

	public DynamicVariableStatement(CharSequence identifier, RequestProperty<? extends IAssignmentExpression> expression) {
		this.identifier = identifier;
		this.expression = expression;
	}

	public VariableStatement getValue(Map model, RequestContext requestContext) throws Exception {
		if (expression == null) {
			return new VariableStatement(identifier);
		}
		else {
			return new VariableStatement(identifier, expression.getValue(model, requestContext));
		}
	}

}
